package com.cnwk;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Random;

public class Panel extends JPanel {
    JLabel title; //标题
    JLabel timeLabel; //次数显示器
    JLabel res; //结果显示器
    JTextField input; //输入框
    JButton run; //运行按钮
    Font titleFont, resFont, inputFont;
    int stage; //阶段控制器
    ActionListener actionListener;
    FocusEvent focusEvent;
    int level; //难度
    String timeShow, result;
    int maxTime; //最大次数
    int nowTime; //当前次数
    String resStr;
    char[] randomNum = {'0', '0', '0', '0', '0', '0', '0', '0'}; //生成的随机数
    char[] inNum; //输入的文本
    int[] rN;
    int[] iN;
    int A, B;

    public Panel() {
        title = new JLabel("猜  数");
        titleFont = new Font("title", Font.BOLD, 100);
        timeLabel = new JLabel();
        res = new JLabel();
        resFont = new Font("res", Font.PLAIN, 30);
        input = new JTextField();
        inputFont = new Font("input", Font.PLAIN, 30);
        run = new JButton();
        actionListener = e -> operation(input.getText());
        run.addActionListener(actionListener);

        title.setFont(titleFont);
        title.setHorizontalAlignment(SwingConstants.CENTER);
        title.setVerticalAlignment(SwingConstants.CENTER);

        timeLabel.setFont(resFont);
        timeLabel.setHorizontalAlignment(SwingConstants.CENTER);

        res.setFont(resFont);
        res.setHorizontalAlignment(SwingConstants.CENTER);

        input.setFont(inputFont);
        input.setHorizontalAlignment(SwingConstants.CENTER);
        input.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                input.setText("");
            }

            @Override
            public void focusLost(FocusEvent e) {

            }
        });


        title.setBounds(0, 0, 500, 300);
        timeLabel.setBounds(0, 250, 500, 50);
        res.setBounds(0, 300, 500, 50);
        input.setBounds(150, 400, 200, 30);
        run.setBounds(200, 500, 100, 50);
        setLayout(null);
        add(title);
        add(timeLabel);
        add(res);
        add(input);
        add(run);
        init();
    }

    private void init() {
        timeLabel.setText("");
        res.setText("请输入难度");
        input.setText("4-8");
        run.setText("确认");
        stage = 1;
    }

    private void operation(String in) {
        input.setText("");
        switch (stage) {
            case 1 -> {
                if (in.length() != 1) {
                    res.setText("不符合条件！");
                    input.setText("请输入难度(4-8)");
                    break;
                }
                if (in.charAt(0) < 52 || in.charAt(0) > 56) {
                    res.setText("不符合条件！");
                    input.setText("请输入难度(4-8)");
                    break;
                }
                level = (int) in.charAt(0) - 48;
                System.out.println("难度设置：" + level);
                res.setText("请输入猜测次数");
                input.setText("1-100");
                stage = 2;
                System.out.println("进入第二阶段");
            }
            case 2 -> {
                if (in.length() == 0 || in.length() > 3) {
                    res.setText("不符合条件！");
                    input.setText("请输入猜测次数(1-100)");
                    break;
                }
                maxTime = toInt(in);
                System.out.println("次数设置：" + maxTime);
                if (maxTime < 1 || maxTime > 100) {
                    res.setText("不符合条件！");
                    input.setText("请输入猜测次数(1-100)");
                    break;
                }
                nowTime = 1;
                timeSet();
                res.setText("请猜数");
                input.setText("");
                stage = 3;
                initGame();
                System.out.println("进入第3阶段");
            }
            case 3 -> judge(in);
            case 4 -> init();
        }
    }
    private void initGame() {
        System.out.println("开始初始化");
        Random r = new Random();
        rN = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int x;
        for(int i = 0; i < level; i++) {
            x = r.nextInt(10);
            rN[x] += 1;
            randomNum[i] = (char)(x + 48);
        }
        resStr = new String(randomNum).substring(0, level);
        System.out.println(resStr);
        System.out.println("初始化完成");
    }
    private int toInt(String in) {
        int res = 0;
        int mul = 1;
        char[] chars = in.toCharArray();
        for(int i = 0; i < in.length(); i++)
            if((int)chars[i] < 48 || (int)chars[i] > 57)
                return -1;
        for(int i = in.length(); i > 0; i--) {
            res += ((int)in.charAt(i - 1) - 48) * mul;
            mul = mul * 10;
        }
        return res;
    }
    private void timeSet() {
        timeShow = "第" + nowTime +"/" + maxTime +"次";
        timeLabel.setText(timeShow);
    }
    private void judge(String in) {
        if(in.length() == level) {
            iN = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            inNum = in.toCharArray();
            for(int i = 0; i < level; i++) {
                if((int)inNum[i] < 48 || (int)inNum[i] > 57) {
                    res.setText("输入的字符应全为数字");
                    input.setText("");
                    return;
                }
                iN[inNum[i] - 48] += 1;
            }
            A = getA();
            if(A == level) {
                stage = 4;
                res.setText("恭喜成功！");
                run.setText("再来一次");
            }
            else {
                B = getB();
                if(nowTime != maxTime) {
                    res.setText(input.getText() + " " + A + "A" + B + "B");
                    input.setText("");
                    nowTime++;
                    timeSet();
                }
                else {
                    timeLabel.setText("次数已用尽！");
                    res.setText("可惜失败了");
                    input.setText("答案是：" + resStr);
                    stage = 4;
                    run.setText("再来一次");
                }
            }

        }
        else {
            res.setText("输入字符应为" + level + "个");
            input.setText("");
        }
    }
    private int getA() {
        int A = 0;
        for(int i = 0; i < level; i++) {
            if(randomNum[i] == inNum[i])
                A++;
        }
        return A;
    }
    private int getB() {
        int B = 0;
        for(int i = 0; i < 10; i++) {
            B += Math.min(rN[i], iN[i]);
        }
        return B - A;
    }
}
